//封装显示界面; e.g.void showMenu
#include <cstdlib>
#include <iostream>
#include <istream>
#include <string>
using namespace std;
#define MAX 1000
/*联系人*/
struct Person
{
    string m_Name;    // 姓名
    int m_Sex;        // 性别 1 男, 2 女
    int m_Age;        // 年龄
    string m_phone;   // 电话
    string m_Addr;    // 地址
};

/*通讯录*/
struct Addressbooks
{
    struct Person personArray[MAX];
    int m_Size;
};

/*添加联系人*/
void addPerson(Addressbooks *ads)
{
    if(MAX == ads->m_Size)
    {
        cout << "通讯录已满!" << endl;
        return;
    }
    else 
    {
        string name;
        cout << "请输入姓名:" << endl;
        cin >> name;
        ads->personArray[ads->m_Size].m_Name = name;

        //性别
        cout << "请输入性别:" << endl;
        cout << "1--男" << endl;
        cout << "2--女" << endl;
        int sex = 0;

        while(true)
        {
            cin >> sex;
            if(1 == sex || 2 == sex)
            {
                ads->personArray[ads->m_Size].m_Sex = sex;
                break;
            }
            cout << "输入有误,请重新输入:" << endl;
        }

        //年龄
        cout << "请输入年龄:" << endl;
        int age = 0;
        while(true)
        {
            cin >> age;
            if(0 <= sex && 200 >= sex)
            {
                ads->personArray[ads->m_Size].m_Age = age;
                break;
            }
            cout << "输入有误,请重新输入:" << endl;
        }

        //电话
        cout << "请输入电话:" << endl;
        string phone;
        cin >> phone;
        ads->personArray[ads->m_Size].m_phone = phone;

        //住址
        cout << "请输入住址:" << endl;
        string address;
        cin >> address;
        ads->personArray[ads->m_Size].m_Addr = address;

        // 更新通讯录人数
        ads->m_Size++;
        cout << "添加成功!" << endl;
        system("pause");
        system("cls");
    }
}

/*显示联系人*/
void showPerson(Addressbooks *ads)
{
    if(0 == ads->m_Size)
    {
        cout << "当前记录为空!" << endl;
    }
    else
    {
        for(int i = 0; i < ads->m_Size; i++)
        {
            cout << "姓名: " << ads->personArray[i].m_Name << "\t";
            cout << "性别: " << ads->personArray[i].m_Sex << "\t";
            cout << "年龄: " << (ads->personArray[i].m_Age == 1 ? "男" : "女")<< "\t";
            cout << "住址: " << ads->personArray[i].m_Addr << endl;
        }
    }
    system("pause");
    system("cls");
}

/*联系人是否存在?,否,返回 -1*/
int isExist(Addressbooks *ads, string name)
{
    for(int i = 0; i < ads->m_Size; i++)
    {
        if(name == ads->personArray[i].m_Name)
        {
            return i;
        }
    }
    return -1;
}

/*删除联系人*/
void deletePerson(Addressbooks *ads)
{
    cout << "请输入要删除的联系人姓名:" << endl;
    string name;
    cin >> name;
    int ret = isExist(ads, name);
    if(-1 == ret)
    {
        for(int i = ret; i < ads->m_Size; i++)
        {
            //move forward
            ads->personArray[i] = ads->personArray[i + 1];
        }
        ads->m_Size--;
        cout << "删除成功!" << endl;
    }
    else 
    {
        cout << "查无此人!" << endl;
    }
}

/*查找联系人*/
void findPerson(Addressbooks *ads)
{
    cout << "请输入要查找的联系人:" << endl;
    string name;
    cin >> name;
    int ret = isExist(ads, name);

    if(-1 != ret)
    {
        cout << "姓名: " << ads->personArray[ret].m_Name << "\t";
        cout << "性别: " << ads->personArray[ret].m_Sex << "\t";
        cout << "年龄: " << (ads->personArray[ret].m_Age == 1 ? "男" : "女")<< "\t";
        cout << "住址: " << ads->personArray[ret].m_Addr << endl;
    }
    else
    {
        cout << "查无此人!" << endl;
    }

    system("pause");
    system("cls");
}

/*修改联系人*/
void modifyPerson(Addressbooks *ads)
{
    cout << "请输入修改联系人的姓名:" << endl;
    string name; 
    cin >> name;
    int ret = isExist(ads, name);

    if(-1 != ret)
    {
        //姓名
        string name;
        cout << "请输入姓名:" << endl;
        cin >> name;
        ads->personArray[ret].m_Name = name;
        //性别
        cout << "请输入性别:" << endl;
        int sex = 0;
        while(true)
        {
            cin >> sex;
            cout << "1--男" << endl;
            cout << "2--女" << endl;
            if(1 == sex || 2 == sex)
            {
                ads->personArray[ret].m_Sex = sex;
                break;
            }
            cout << "输入有误,请重新输入:" << endl;
        }
        
        cout << "请输入年龄:" << endl;
        int age = 0;
        while(true)
        {
            cin >> age;
            if(0 <= sex && 200 >= sex)
            {
                ads->personArray[ads->m_Size].m_Age = age;
                break;
            }
            cout << "输入有误,请重新输入:" << endl;
        }

        //电话
        cout << "请输入电话:" << endl;
        string phone;
        cin >> phone;
        ads->personArray[ads->m_Size].m_phone = phone;

        //住址
        cout << "请输入住址:" << endl;
        string address;
        cin >> address;
        ads->personArray[ads->m_Size].m_Addr = address;

        cout << "修改成功!" << endl;
        system("pause");
        system("cls");
    }
    else
    {
        cout << "查无此人!" << endl;
    }
    system("pause");
    system("cls");
}

void cleanPerson(Addressbooks *ads)
{
    ads->m_Size = 0;
    cout << "通讯录已经清空!" << endl;
    system("pause");
    system("cls");
}

/*菜单*/
void showMenu()
{
    cout << "*****  1.添加联系人  *****" << endl;
    cout << "*****  2.显示联系人  *****" << endl;
    cout << "*****  3.删除联系人  *****" << endl;
    cout << "*****  4.查找联系人  *****" << endl;
    cout << "*****  5.修改联系人  *****" << endl;
    cout << "*****  6.清空联系人  *****" << endl;
    cout << "*****  0.退出通讯录  *****" << endl;
}

int main(void)
{
    Addressbooks ads;
    int select = 0;
    while(true)
    {
        //显示菜单
        showMenu();

        cin >> select;
        switch(select)
        {
            case 1:
                //添加联系人
                addPerson(&ads);
                break;
            case 2:
                //显示联系人
                showPerson(&ads);
                break;
            case 3:
                //删除联系人
                deletePerson(&ads);
                /*
                {
                    cout << "请输入删除联系人的姓名:" << endl;
                    string name;
                    cin >> name;
                    
                    if(-1 == isExist(&ads, name))
                    {
                        cout << "查无此人!" << endl;
                    }
                    else
                    {
                        cout << "找到此人!" << endl;
                    }
                }
                */
                break;
            case 4:
                //查找联系人
                findPerson(&ads);
                break;
            case 5:
                //修改联系人
                modifyPerson(&ads);
                break;
            case 6:
                //清空联系人
                cleanPerson(&ads);
                break;
            case 0:
                cout<< "欢迎使用" << endl;
                system("pause");
                return 0;
                break;
            default:
                break;        
        }
    }

    system("pause");
    return 0;
}